package com.xiaocui.utils;

import com.xiaocui.domain.Point;
import org.apache.log4j.Logger;

/**
 * 火星坐标系和百度坐标系互转
 */
public class TransformPointer_mars2bd {
    private static final Logger log = Logger.getLogger(TransformPointer_mars2bd.class);

    public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * gcj_bd
     *
     * @param gg_lat
     * @param gg_lon
     * @param bd_lat
     * @param bd_lon
     */
    void bd_encrypt(double gg_lat, double gg_lon, double bd_lat, double bd_lon) {
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;
    }

    // bd_gcj
    void bd_decrypt(double bd_lat, double bd_lon, double gg_lat, double gg_lon) {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        gg_lon = z * Math.cos(theta);
        gg_lat = z * Math.sin(theta);
    }

    /**
     * 百度坐标系转为火星坐标系
     *
     * @param pointbd
     * @return
     */
    public static Point bd2mars(Point pointbd) {
        if (pointbd != null) {
            double x = pointbd.getLng() - 0.0065;
            double y = pointbd.getLat() - 0.006;
            double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
            double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
            double mar_lon = z * Math.cos(theta);
            double mar_lat = z * Math.sin(theta);
            Point pointmars = new Point(mar_lon, mar_lat);
            return pointmars;
        }

        return null;
    }

    /**
     * 火星坐标系转为百度坐标系
     *
     * @param pointmars
     * @return
     */
    public static Point mars2bd(Point pointmars) {
        if (pointmars != null) {
            double x = pointmars.getLng();
            double y = pointmars.getLat();
            double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
            double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
            double bd_lon = z * Math.cos(theta) + 0.0065;
            double bd_lat = z * Math.sin(theta) + 0.006;
            Point pointbd = new Point(bd_lon, bd_lat);
            return pointbd;
        }
        return null;

    }
}
